/****************************************************************************
**
** Copyright (C) 2009 Alexander Solis.
**
** This file is part of Alejandria
** Alejandria is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Alejandria is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Alejandria.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include "newbookdialog.h"

#include <QMessageBox>

NewBookDialog::NewBookDialog(QWidget *parent)
	: QDialog(parent)
{
	setupUi(this);

    loadComboBoxData();

    connect(buttonBox, SIGNAL(accepted()), this, SLOT(validateSlot()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
}

void NewBookDialog::validateSlot()
{
	// Validate the dialog fields
	if (lineEdit_id->text().isEmpty()) {
		QMessageBox::information(this, tr("ISBN/ID empty"), tr("You must enter an ISBN or ID for you book."));
		return;
	}
	
	// Prepares and sends the data to the model
	// TODO: verify if this is the correct form to preserve the model/view architecure when sending data to the model
	emit addBookSignal(lineEdit_id->text(), lineEdit_title->text(), lineEdit_author->text(), comboBox_category->itemText(comboBox_category->currentIndex()), comboBox_status->itemText(comboBox_status->currentIndex()), comboBox_own->itemText(comboBox_own->currentIndex()), spinBox_rating->value(), checkBox_loaned->isChecked(), textEdit_comments->toPlainText());

	close();
}

void NewBookDialog::loadComboBoxData()
{
    // Loads book cathegories according to Dewey Decimal classes book classification
    QStringList comboData;
    comboData << "" << "Computer science, information & general works"  << "Philosophy and psychology"  << "Religion"  << "Social sciences"  
        << "Language"  << "Science"  << "Technology"  << "Arts and recreation"  << "Literature"  << "History, geography and biography";
    comboBox_category->addItems(comboData);

    comboData.clear();
    comboData << "" << "Read" << "Not Read" << "Reading" << "N/A";
    comboBox_status->addItems(comboData);

    comboData.clear();
    comboData << "" << "Yes" << "No" << "Wish";
    comboBox_own->addItems(comboData);
}